package 多线程09_原子访问;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 * @ClassName: TestThreadAtom
 * @Author: AllenSun
 * @Date: 2022/3/18 下午11:51
 */
public class TestThreadAtom03 {

    //创建long型原子计数器
    private static AtomicLong atomicLong = new AtomicLong();
    private static LongAdder longAdder = new LongAdder();
    //创建数据源
    private static Integer[] arrayOne = new Integer[]{0,1,2,3,4,0,5,6,0,56};
    private static Integer[] arrayTwo = new Integer[]{10,1,2,3,4,0,5,6,0,56};

    public static void main(String[] args) throws InterruptedException {
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                int size = arrayOne.length;
                for (int i = 0; i < size; ++i) {
                    if(arrayOne[i].intValue()==0){
                        atomicLong.incrementAndGet();
                    }
                }
            }
        });

        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                int size = arrayTwo.length;
                for (int i = 0; i < size; ++i) {
                    if(arrayTwo[i].intValue()==0){
                        atomicLong.incrementAndGet();
                    }
                }
            }
        });

        threadOne.start();
        threadTwo.start();

        threadOne.join();
        threadTwo.join();

        System.out.println("所有数组中0的个数："+atomicLong.get());
    }
}
